package com.hospital.appointment.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hospital.appointment.entity.Statistics;
import com.hospital.appointment.entity.UserInfo;
import com.hospital.appointment.entity.dto.EchartDto;
import com.hospital.appointment.entity.dto.StatisticsDto;
import com.hospital.appointment.service.StatisticsService;
import com.hospital.appointment.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>标题: 统计 - 请求控制层</p>
 *
 * @version: 1.0
 * @author: mic
 * @date 2022-3-3
 */
@RestController
@RequestMapping("/statistics")
@CrossOrigin
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/findList")
    public List<Statistics> findList(Statistics statistics) {
        return statisticsService.findList(statistics);
    }

    @GetMapping("/findListByPage")
    public Page<Statistics> findListByPage(StatisticsDto statistics, Page page) {
        QueryWrapper<Statistics> queryWrapper = new QueryWrapper<Statistics>(statistics);
        if (statistics.getDateStart() != null && statistics.getDateEnd() != null) {
            queryWrapper.gt("create_time", statistics.getDateStartWithDate());
            queryWrapper.le("create_time", statistics.getDateEndWithDate());
        }
        Page result = statisticsService.page(page, queryWrapper);
        return result;
    }

    @GetMapping("/statistic")
    public Map<String, List<EchartDto>> statistic() {
        List<Statistics> statistics = statisticsService.findList(new Statistics());
        Map<String, List<EchartDto>> map = Maps.newHashMap();
        List<EchartDto> dateDto = Lists.newArrayList();
        Map<Date, List<Statistics>> dateMaps = statistics.stream().collect(Collectors.groupingBy(Statistics::getCreateTime));
        for (Date date : dateMaps.keySet()) {
            dateDto.add(new EchartDto(DateUtil.formatDate(date), dateMaps.get(date).stream().mapToInt(Statistics::getValue).sum()));
        }
        dateDto.sort(Comparator.comparing(EchartDto::getX));
        map.put("date", dateDto);

        List<EchartDto> docDto = Lists.newArrayList();
        Map<String, List<Statistics>> docMap = statistics.stream().collect(Collectors.groupingBy(Statistics::getDoctorName));
        for (String doc : docMap.keySet()) {
            docDto.add(new EchartDto(doc, docMap.get(doc).stream().mapToInt(Statistics::getValue).sum()));
        }
        docDto.sort(Comparator.comparing(EchartDto::getY).reversed());
        map.put("doc", docDto);

        List<EchartDto> patientDto = Lists.newArrayList();
        Map<String, List<Statistics>> patientMap = statistics.stream().collect(Collectors.groupingBy(Statistics::getPatientIds));
        for (String patient : patientMap.keySet()) {
            UserInfo info = userInfoService.getById(patient);
            if (info == null) {
                continue;
            }
            patientDto.add(new EchartDto(info.getNickName(), patientMap.get(patient).stream().mapToInt(Statistics::getValue).sum()));
        }
        patientDto.sort(Comparator.comparing(EchartDto::getY).reversed());
        map.put("patient", patientDto);
        return map;
    }

    @GetMapping("/{id}")
    public Statistics getById(@PathVariable String id) {
        return statisticsService.getById(id);
    }

    @PostMapping("/save")
    public Serializable save(Statistics statistics) {
        statisticsService.save(statistics);
        return statistics.getId();
    }

    @PostMapping("/edit")
    public boolean edit(Statistics statistics) {
        return statisticsService.updateById(statistics);
    }

    @GetMapping("/delete/{id}")
    public boolean removeById(@PathVariable Serializable id) {
        return statisticsService.removeById(id);
    }

}
